package GUI;


import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.*;
import javax.swing.border.EmptyBorder;

import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class mainMenu extends JFrame {

	private JPanel contentPane;
	private static debtors debtorsM = new debtors();
	private static cases casesM = new cases();
	private static clients clientsM = new clients();
	private static employees employeesM = new employees();
	private static vehicles vehiclesM = new vehicles();
	private static contacts contactsM = new contacts();
	private logIn logInM;
	private JTextField txtUser;
	private String accessType = "";

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					mainMenu frame = new mainMenu();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public mainMenu() {
		setIconImage(Toolkit.getDefaultToolkit().getImage(mainMenu.class.getResource("/pictures/icon.jpg")));
		setTitle("MAIN MENU");
		setFont(new Font("Times New Roman", Font.PLAIN, 12));
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 900, 500);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JButton casesButton = new JButton("Cases");
		casesButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0){
				casesM.setUser(txtUser.getText());
				casesM.setTableUser(txtUser.getText());
				casesM.updateCaseTable();
				casesM.setFrame(casesM);
				casesM.setAccessType(accessType);
				casesM.setVisible(true);
			}
		});
		
		JButton contactButton = new JButton("Contacts");
		contactButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				contactsM.updateContactTable();
				contactsM.setVisible(true);
			}
		});
		contactButton.setFont(new Font("Times New Roman", Font.BOLD, 27));
		contactButton.setBounds(41, 280, 181, 40);
		contentPane.add(contactButton);
		
		txtUser = new JTextField();
		txtUser.setEditable(false);
		txtUser.setHorizontalAlignment(SwingConstants.CENTER);
		txtUser.setFont(new Font("Times New Roman", Font.PLAIN, 14));
		txtUser.setBounds(702, 48, 167, 26);
		contentPane.add(txtUser);
		txtUser.setColumns(10);
		
		JLabel lblLoggedInAs = new JLabel("Logged in as :");
		lblLoggedInAs.setHorizontalAlignment(SwingConstants.CENTER);
		lblLoggedInAs.setFont(new Font("Times New Roman", Font.ITALIC, 14));
		lblLoggedInAs.setBounds(702, 24, 167, 26);
		contentPane.add(lblLoggedInAs);
		casesButton.setFont(new Font("Times New Roman", Font.BOLD, 27));
		casesButton.setBounds(41, 60, 181, 40);
		contentPane.add(casesButton);
		
		JButton vehiclesButton = new JButton("Vehicles");
		vehiclesButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				vehiclesM.updateVehicleTable();
				vehiclesM.setVisible(true);
			}
		});
		vehiclesButton.setFont(new Font("Times New Roman", Font.BOLD, 27));
		vehiclesButton.setBounds(41, 115, 181, 40);
		contentPane.add(vehiclesButton);
		
		JButton debtorsButton = new JButton("Debtors");
		debtorsButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				debtorsM.updateDebtorTable();
				debtorsM.setVisible(true);
				
			}
		});
		debtorsButton.setFont(new Font("Times New Roman", Font.BOLD, 27));
		debtorsButton.setBounds(41, 170, 181, 40);
		contentPane.add(debtorsButton);
		
		JButton clientsButton = new JButton("Clients");
		clientsButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				clientsM.updateClientTable();
				clientsM.setVisible(true);
			}
		});
		clientsButton.setFont(new Font("Times New Roman", Font.BOLD, 27));
		clientsButton.setBounds(41, 225, 181, 40);
		contentPane.add(clientsButton);
		
		JButton employeesButton = new JButton("Employees");
		employeesButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				employeesM.updateEmployeeTable();
				//setAccessType(accessType);
				employeesM.setAccessType(accessType);
				employeesM.setVisible(true);
			}
		});
		employeesButton.setFont(new Font("Times New Roman", Font.BOLD, 27));
		employeesButton.setBounds(41, 335, 181, 40);
		contentPane.add(employeesButton);
		
		JButton logOutButton = new JButton("Log out");
		logOutButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				logInM.setVisible(true);
				setVisible(false);
				debtorsM.setVisible(false);
				casesM.setVisible(false);
				clientsM.setVisible(false);
				employeesM.setVisible(false);
				vehiclesM.setVisible(false);
				clearTextFields();
			}
		});
		logOutButton.setFont(new Font("Times New Roman", Font.PLAIN, 20));
		logOutButton.setBounds(15, 420, 116, 30);
		contentPane.add(logOutButton);
		
		JLabel label = new JLabel("");
		label.setIcon(new ImageIcon(mainMenu.class.getResource("/pictures/LMS.jpg")));
		label.setBounds(0, 0, 884, 462);
		contentPane.add(label);
	}
	
	public void setLoggedIn(String _username)
	{
		txtUser.setText(_username);
	}
	
	public void setLogInM(logIn _logIn)
	{
		logInM = _logIn;
	}
	
	public void setAccessTypeVariable(String _accessType)
	{
		accessType = _accessType;
	}
	
	public void setAccessType(String _accessType)
	{
		employeesM.setAccessType(_accessType);
		casesM.setAccessType(_accessType);
	}
	
	public void clearTextFields()
	{
		txtUser.setText("");
		employeesM.clearTextFields();
		clientsM.clearTextFields();
		vehiclesM.clearTextFields();
		debtorsM.clearTextFields();
		casesM.clearTextFields();
	}
}
